package com.experiment.aicommunityback.controller;

import com.alibaba.fastjson.JSON;
import com.experiment.aicommunityback.entity.User;
import com.experiment.aicommunityback.entity.Result;
import com.experiment.aicommunityback.service.RoleService;
import com.experiment.aicommunityback.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import sun.tools.jar.Main;

import java.util.Map;

import static com.experiment.aicommunityback.entity.ResultCodeEnum.*;

/**
 * @Author: 杨婉汐
 * @name: UserController
 * @Date: 2024/12/20  16:54
 */
@RestController
@RequestMapping("/home")
@CrossOrigin()
public class UserController {
    UserService userService;
    RoleService roleService;
//    HdfsSrevice hdfsSrevice;

    @Autowired
    public UserController(UserService userService, RoleService roleService) {
        this.userService = userService;
        this.roleService= roleService;
    }


    /**
     * 查询用户表,展示用户信息
     * <br>
     *
     * @return data(" userList ", userList).data(" urList ", urList)
     */
    @GetMapping("/userList")
    public Result showUser() {
        System.out.println("展示所有用户及其附带的最高权限");
        return userService.showUser();
    }

    /**
     * 根据Id或Name查询
     */
    @PostMapping("/queryUser")
    public Result queryUser(@RequestParam String idOrName) {
        User queryUser = userService.queryUser(idOrName);
        String roleCode=userService.queryRoleCode(queryUser.getId());
        if (queryUser != null) {
            return Result.ok().data("queryUser", queryUser).data("codeOfUser",roleCode);
        }
        return Result.setResult(QUERY_USER_ERROR);
    }

    /**
     * 新增用户角色
     */
    @PostMapping("/addUser")
    public Result addUser(@RequestBody String josnText) {
        User user = JSON.parseObject(josnText, User.class);
        System.out.println("准备新增用户:" + user.toString());
        return userService.addUser(user);
    }


    /**
     * 更改用户
     */
    @PostMapping("/updatedUser")
    public Result updatedUser(@RequestBody String jsonText) {
        User User = JSON.parseObject(jsonText, User.class);
        return userService.updatedUser(User);

    }

    /**
     * 更新用户状态
     */
    @PostMapping("/updatedUserStatus")
    public Result updatedUserStatus(@RequestBody Map<String, Object> paramMap) {
        System.out.println(paramMap);
        userService.updatedUserStatus(paramMap);
        return null;

    }

    /**
     * 删除用户
     */
    @PostMapping("/deleteUser")
    public Result deleteUser(@RequestParam String id) {
        Boolean delete = userService.deleteUser(Integer.parseInt(id));
        return delete ? Result.ok().data("deleteUser", true) : Result.setResult(DELETE_USER_ERROR);
    }

    /**
     * 头像测试
     *
     * @return
     */
    @GetMapping("/demo")
    public void demo() {

    }


    /**
     * 为用户添加权限角色<br>1.返回可选的权限
     * */
    @GetMapping("/userAddRole")
    public Result getOptionalRole() {
        return roleService.optionalRole();
    }


    /**
     * 为用户添加权限角色<br>2.接受参数并进行保存
     * */
    @PostMapping("/userAddRole")
    public Result userUpdatedRole(@RequestParam int userId, @RequestParam("roleName") String newRoleName) {
        return userService.userUpdatedRole(userId, newRoleName);
    }

    /**
     * <Strong>未实现</Strong><hr>
     * 为用户添加权限角色<br>2.接受参数并进行保存
     * */
//    @PostMapping("/userAddRole")
//    public Result userAddRole(int userid,String[] userRoles) {
//        // userid role
//        return userService.userAddRole(userid,demoData);
//        return null;
//    }


}
